#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   For_Jenkins_2_test.py
@Time    :   2022/08/31 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import FALSE, TRUE
import struct
from pytest_assume.plugin import assume
 
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

import project_CAN
import Set_up

HostSpeedY_dict = {  -1      :   -13.6125,
                    -0.9    :   -12.2625,
                    -0.8    :   -10.9125,
                    -0.7    :   -9.5626,
                    -0.6    :   -8.2125,
                    -0.5    :   -6.8625,
                    -0.4    :   -5.5125,
                    -0.3    :   -4.1625,
                    -0.2    :   -2.8125,
                    -0.1    :   -1.4625,
                    0.0     :   -0.1125,
                    0.1     :   1.35,
                    0.2     :   2.7,
                    0.3     :   4.05,
                    0.4     :   5.4,
                    0.5     :   6.75,
                    0.6     :   8.1,
                    0.7     :   9.45,
                    0.8     :   10.8,
                    0.9     :   12.15,
                    1.0     :   13.5
                }


class Test_BYD_HostSpeed(object):
    @allure.severity('normal')
    @allure.title('Test Read MRR HostSpeedX with speed rise From64E in default session with physical addressing')
    @allure.description('Test Read MRR HostSpeedX with speed rise From64E in default session with physical addressing')
    # @pytest.mark.repeat(200)
    def test_Read_MRR_HostSpeedX_HostSpeedY_Speed_Rise_phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Offset_S',0,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Status_S',1,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Signal_S',0,iStep)
        for i in range(20, 160, 10):
            Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep)
            #设置车速大于3KM/H
            iStep = Driver['CAN'].setSignalValue('IPB_0x121','IPB_Vehicle_Speed_S',i,iStep)
            iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
            time.sleep(2)

            HostSpeedX,HostSpeedY = self.readMrrHostSpeedxAndHostSpeedY(Driver) 

            with assume: assert i-1 < HostSpeedX < i+1

        iStep = Driver['CAN'].setSignalValue('IPB_0x121','IPB_Vehicle_Speed_S',0,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Offset_S',-0.13,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Status_S',1,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Signal_S',-2.0943,iStep)

    
    @allure.severity('normal')
    @allure.title('Test Read MRR HostSpeedX with speed rise From64E in default session with physical addressing')
    @allure.description('Test Read MRR HostSpeedX with speed rise From64E in default session with physical addressing')
    # @pytest.mark.repeat(200)
    def test_Read_MRR_HostSpeedX_HostSpeedY_Signal_Rise_phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Offset_S',0,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Status_S',1,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x121','IPB_Vehicle_Speed_S',20,iStep)
        for i in range(-10, 11, 1):
            Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep)
            #设置车速大于3KM/H
            iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Signal_S',i/10,iStep)
            iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
            time.sleep(2)

            HostSpeedX,HostSpeedY = self.readMrrHostSpeedxAndHostSpeedY(Driver) 
            value = HostSpeedY_dict[i/10]
            logging.info('Check value: %f', value)

            with assume: assert  value - 1< HostSpeedY < value + 1

        iStep = Driver['CAN'].setSignalValue('IPB_0x121','IPB_Vehicle_Speed_S',0,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Offset_S',-0.13,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Status_S',1,iStep)
        iStep = Driver['CAN'].setSignalValue('IPB_0x222','Yaw_Rate_Signal_S',-2.0943,iStep)

    
    def readMrrHostSpeedxAndHostSpeedY(self,Driver):
        Driver['CAN2'].fush_q_buff()  # 清缓存  
        time.sleep(0.1)

        ALl_data,buffer_size = Driver['CAN2'].read_frame()
        response_data = ALl_data[ALl_data['AIdentifier'] == 0x64E]
        respData = response_data.iloc[0]['ADatas'][:16]

        actuRespStr = binascii.b2a_hex(bytearray(respData), ' ') 
        logging.info('P_CAN responseID:[0x{0:X}], Data:[{1}]'.format(0x64E, actuRespStr))
        byte2 = respData[6] & 0x1F
        byte1 = respData[5]
        HostSpeedX = byte2 << 8 | byte1
        HostSpeedX = (HostSpeedX * 0.03125 -128) * 3600 / 1000  
        logging.info('MRR HostSpeedX:[{0}]'.format(HostSpeedX))
             

        response_data = ALl_data[ALl_data['AIdentifier'] == 0x66F]
        respData = response_data.iloc[0]['ADatas'][:16]

        actuRespStr = binascii.b2a_hex(bytearray(respData), ' ') 
        logging.info('P_CAN responseID:[0x{0:X}], Data:[{1}]'.format(0x66F, actuRespStr))
        byte3 = respData[15] & 0x07
        byte2 = respData[14]
        byte1 = (respData[13] & 0xC0) >> 6
        HostSpeedY = byte3 << 10 | byte2 << 2 | byte1
        HostSpeedY = (HostSpeedY * 0.03125 -128) * 3600 / 1000  
        logging.info('MRR HostSpeedY:[{0}]'.format(HostSpeedY))

        return HostSpeedX,HostSpeedY